#include "stdafx.h"
#include "AppDelegate.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "cocos-ext.h"
#include "CCArmature/utils/CCArmatureDataManager.h"
#include "map.h"
#include "role.h"


USING_NS_CC;
using namespace CocosDenshion;



static Role* g_main_role = NULL;


class GameLayer : public CCLayer
{
public:
	static GameLayer* Create()
	{
		GameLayer *pRet = new GameLayer();
		if (pRet && pRet->init())
		{
			pRet->setAnchorPoint(ccp(0,0));
			pRet->setPosition(ccp(0,0));
			pRet->autorelease();
			return pRet;
		}
		else
		{
			CC_SAFE_DELETE(pRet);
			return NULL;
		}
	}

	GameLayer()
		:map_(NULL)
	{

	}
	virtual ~GameLayer()
	{
		if(map_)
		{
			Map::Destory(map_);
		}
	}

	void SetMap(Map* map)
	{
		if(map_)
		{
			this->removeChild(map_->GetNode());
			Map::Destory(map_);
		}
		map_ = map;
		if(map_)
		{
			this->addChild(map_->GetNode());
		}
	}

	Map* GetMap()
	{
		return map_;
	}

	virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
	{
		return;//
		CCTouch *touch = (CCTouch*)pTouches->anyObject();
		CCPoint diff = touch->getDelta();
		Pos pos = map_->GetPos();
		pos.x_ += diff.x;
		pos.y_ += diff.y;
		map_->SetPos( pos );
	}

	virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
	{
		CCTouch *touch = (CCTouch*)pTouches->anyObject();
		if(g_main_role)
		{
			CCPoint diff = ccpSub(touch->getLocation() , touch->getStartLocation());
			//if(diff.x == 0 && diff.y == 0)  
			{
				CCPoint pt = touch->getLocation();
				Pos pos_target = map_->GetPos();
			
				pos_target.x_ = pt.x - pos_target.x_;
				pos_target.y_ = pt.y - pos_target.y_;

				if(map_->CanMoveTo(pos_target))
				{
					Pos world_pos = g_main_role->GetPos();
					pos_target.x_ -= world_pos.x_;
					pos_target.y_ -= world_pos.y_ ;

					g_main_role->MoveBy(pos_target);
				}

				
			}
		}
	}
private:
	 Map* map_;
};


class UILayer : public CCLayer
{
public:
	//todo...
};



class GameScene : public CCScene
{
public:

	enum GameLayerIndex
	{
		k_game_layer = 0,
		k_ui_layer,
	};

	GameScene()
		:game_layer_(NULL)
		,ui_layer_(NULL)
	{

	}

	virtual ~GameScene()
	{
		CC_SAFE_RELEASE(game_layer_);
		CC_SAFE_RELEASE(ui_layer_);
	}
	static GameScene* GameScene::Create()
	{
		GameScene *pRet = new GameScene();
		if (pRet && pRet->init())
		{
			pRet->autorelease();
			return pRet;
		}
		else
		{
			CC_SAFE_DELETE(pRet);
			return NULL;
		}
	}
	void SetGameLayer(GameLayer* game_layer)
	{
		if(game_layer_)
		{
			this->removeChild(game_layer_);
			game_layer_->release();
		}
		game_layer_ = game_layer;
		if(game_layer_)
		{
			game_layer_->retain();
			this->addChild(game_layer_,k_game_layer,k_game_layer);
		}
	}
	void SetUILayer(UILayer* ui_layer)
	{
		if(ui_layer_)
		{
			this->removeChild(ui_layer_);
			ui_layer_->release();
		}
		ui_layer_ = ui_layer;
		if(ui_layer_)
		{
			ui_layer_->retain();
			this->addChild(ui_layer_,k_ui_layer,k_ui_layer);
		}
	}
private:
	GameLayer*	game_layer_;	
	UILayer*	ui_layer_;
};

void SetupScene(GameScene* game_scene)
{
	//game_scene
	if(game_scene)
	{
		GameLayer* game_layer = GameLayer::Create();
		game_layer->setTouchEnabled(true);

		MapInfo info;
		Map* map = Map::Create(info);
		game_layer->SetMap(map);
		game_scene->SetGameLayer(game_layer);

		//add role,todo...
		RoleInfo role_info;
		Role* role = Role::Create(role_info);
		map->AddRole(role);
		//main role to control
		g_main_role = role;

		//camera to follow role
		map->FollowRole(role);


		//add other 
		RoleInfo role_info2;
		Role* role2 = Role::Create(role_info);
		map->AddRole(role2);
		role2->SetPos(Pos(200,200));
	}
}

//---------------------------------------------------------

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{

//    SimpleAudioEngine::end();
	cocos2d::extension::CCArmatureDataManager::purgeArmatureSystem();
}

bool AppDelegate::applicationDidFinishLaunching()
{
	// As an example, load config file
	// XXX: This should be loaded before the Director is initialized,
	// XXX: but at this point, the director is already initialized
	//CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-example.plist");
    // initialize director
	CCDirector *pDirector = CCDirector::sharedDirector();
	pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    CCSize screenSize(960,640);
	CCEGLView::sharedOpenGLView()->setFrameSize(screenSize.width,screenSize.height);

    CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils();
   
	std::vector<std::string> search_path;
	search_path.push_back("./");
    pFileUtils->setSearchPaths(search_path);

    pDirector->setContentScaleFactor(1);
    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(screenSize.width, screenSize.height, kResolutionNoBorder);

	// turn on display FPS
	pDirector->setDisplayStats(true);

	// set FPS. the default value is 1.0/60 if you don't call this
	pDirector->setAnimationInterval(1.0 / 60);

	//-------------------------------------------------------------------------
	//begin game space...
	GameScene* game_scene = GameScene::Create();
	SetupScene(game_scene);
	pDirector->runWithScene(game_scene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->stopAnimation();
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->startAnimation();
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
